<?php

namespace App\Http\Controllers\Web;

use App\Repositories\TalentRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class TalentController extends Controller
{
    private $talentRepository;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(TalentRepository $talentRepository)
    {
        $this->middleware('auth');
        $this->talentRepository = $talentRepository;
    }

    public function index(Request $request) {
        $main_number = $request->main_number;
        $condition = [];
        if($main_number){
            $condition = [
                'main_number' => $main_number
            ];
        }

        $list = $this->talentRepository->paginateWithCondition(10,[],$condition);
        return view('talent/index',compact('list', 'condition'));
    }

    public function create(Request $request) {

        return view('talent/create');
    }

    public function edit(Request $request, $id) {
        $talent = $this->talentRepository->find($id);
        return view('talent/edit', compact('talent'));
    }

    public function update(Request $request) {
        $talent = $this->talentRepository->find($request->id);

        $newTalent = $this->talentRepository->update($talent, $request->all());
        return redirect(route('talent_show',['data' => $newTalent]));
    }

    public function show(Request $request, $id) {
        $talent = $this->talentRepository->find($id);
//        $talent = $this->talentRepository->findByWhere($id);
        return view('talent/detail',compact('talent'));
    }

    public function store(Request $request) {
        $ret = $this->talentRepository->create($request->all());
        return redirect(route('talent_show',['data' => $ret]));
    }
    
    public function delete ($id) {
        $talent = $this->talentRepository->find($id);
        $ret = $this->talentRepository->destroy($talent);

        return response()->json([
            'errors' =>  false,
            'data' => $ret
        ]);
    }


    public function relation(Request $request , $id) {
        $talent = $this->talentRepository->find($id, ['relationships']);
        return view('talent/relation',compact('talent'));
    }
}
